package cn.wolfcode.trip.api.controller;

import cn.wolfcode.trip.base.domain.News;
import cn.wolfcode.trip.base.domain.NewsComment;
import cn.wolfcode.trip.base.query.CommentQueryObject;
import cn.wolfcode.trip.base.query.NewsQuery;
import cn.wolfcode.trip.base.service.INewsCommentService;
import cn.wolfcode.trip.base.service.INewsService;
import cn.wolfcode.trip.base.util.JsonResult;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/news")
public class NewsController {

    @Autowired
    private INewsService newsService;
    @Autowired
    private INewsCommentService newsCommentService;
    @RequestMapping
    public PageInfo list(NewsQuery qo)
    {
        //qo.setPageSize(6);
      return   newsService.query(qo);
    }
    @RequestMapping("{id}")
    public News getById(@PathVariable Long id)
    {
      return   newsService.getById(id);
    }

    @PutMapping
    public JsonResult updateBrowseNum(News news)
    {
        JsonResult result = new JsonResult();
        try {
            //关联当前用户
            newsService.updateBrowseNum(news);
        } catch (Exception e) {
            e.printStackTrace();
            result.mark(e.getMessage());
        }
        return result;
    }

    /**
     * 查询指定游记下的所有评论信息
     *
     * @param qo
     * @return
     */
    @GetMapping("/{newsId}/comments")
    public PageInfo<NewsComment> listCommonds(CommentQueryObject qo) {
        PageInfo pageInfo = newsCommentService.query(qo);
        return pageInfo;
    }

}
